/**
    @project {
        "templateId": "org.helgoboss.custom",
        "version": "0.1-SNAPSHOT",
        "dependencies": [
            "org.scala-lang:scala-library:2.9.0-1",
            "org.helgoboss:unlocker:0.1-SNAPSHOT",
            "org.helgoboss:scala-additions:0.1-SNAPSHOT",
            "org.helgoboss:osgi-additions:0.1-SNAPSHOT",
            "org.helgoboss:win32-facade:0.2-SNAPSHOT"
        ]
    }
*/

package org.helgoboss.unlocker_ref_impl

import sys.process._
import org.helgoboss.unlocker.Unlocker
import java.io.File
import org.helgoboss.scala_additions.Implicits._
import org.helgoboss.scala_additions.CurrentPlatform
import org.helgoboss.scala_additions.OperatingSystemClass._
import org.helgoboss.osgi_additions._
import org.helgoboss.win32_facade.Win32Facade
import org.helgoboss.osgi_additions.metatype._

class Activator extends OsgiProvider with OsgiConfigurationWatcher with OsgiMetaTypeProvider {
    whenBundleActive {        
        provideMetaType(objectClass)
        
        whenConfigurationUpdated(objectClass.id) { conf =>
            val resultConf = conf match {
                case Some(c) => objectClass.defaultConfig ++ c
                case None => objectClass.defaultConfig
            }
            val exeFile = new File(resultConf("exeFile").asInstanceOf[String])
            new UnlockerRefImpl(exeFile).providesService[Unlocker]
        }
    }
    
    lazy val objectClass = ObjectClass (
        id = "org.helgoboss.unlocker",
        name = "Unlocker",
        requiredAttributes = List (
            ElementaryAttribute[String](id = "exeFile", name = "Unlocker executable path", default = Some(defaultExeFile))
        )
    )
    
    val defaultExeFile = (Win32Facade.getProgramFilesDir / "Unlocker/Unlocker.exe").windowsPathExpression
}


class UnlockerRefImpl(exeFile: File) extends Unlocker {
    def unlock(file: File) {
        Process(List(exeFile.windowsPathExpression, file.windowsPathExpression)).run
    }
}